﻿#include "Measure_TSS.h"
#include "qdebug.h"
Measure_TSS::Measure_TSS()
{

}
int Measure_TSS::M_TSS(HObject Img,HTuple &OutPara,HObject &OutImg)
{
    int flag=0;
    Flag=true;
    float para1=m_part.para1;
    //水平
    float para2=0;

    //——————————————————————垂直方向
    //补偿值
    float para3 = m_part.para3;
    //标准值
    float para4 = m_part.para4;
    //上限值
    float para5 = m_part.para5;
    //下限值
    float para6 = m_part.para6;




    //------------------图像处理

    //粗定定位阈值
    float para17=m_part.para17;
    //找边sigma
    float para19=m_part.para19;
    //找边阈值
    float para20=m_part.para20;







    // Local iconic variables
    HObject  Region, ROI_0, ImageReduced;
    HObject  ImageRotate, RegionTrans, Rectangle1, ImageReduced1;
    HObject  Edges1;

    // Local control variables
    HTuple  Row, Column, Value, Length1;
    HTuple  Length2, Area, C_R, C_C, H, W;
    HTuple  Max_R, Max_C, RowBegin1, ColBegin1;
    HTuple  RowEnd1, ColEnd1, Nr1, Nc1, Dist1;
    HTuple  Index1, MetrologyHandle1, Index, Parameter;
    HTuple  Line1, Line2, D1, D2, DD1;


    try
    {

		Flag11 = true;

        GenRectangle1(&ROI_0, 92.96, 121.023, 1068.53, 2122.59);

        ReduceDomain(Img, ROI_0, &ImageReduced);
        Threshold(ImageReduced, &Region, 0, para17);
        Connection(Region, &Region);
        SelectShapeStd(Region, &Region, "max_area", 70);
        SmallestRectangle2(Region, &Row, &Column, &Value, &Length1, &Length2);

        if (0 != (Value>(PI/2)))
        {
            RotateImage(Img, &ImageRotate, (((PI/2)-Value)*180)/PI, "constant");
        }
        else
        {
            RotateImage(Img, &ImageRotate, ((-Value)*180)/PI, "constant");
        }


        Threshold(ImageRotate, &Region, 0, para17);
        Connection(Region, &Region);
        SelectShapeStd(Region, &Region, "max_area", 70);
        ShapeTrans(Region, &RegionTrans, "rectangle2");
        AreaCenter(RegionTrans, &Area, &C_R, &C_C);
        RegionFeatures(RegionTrans, "height", &H);
        RegionFeatures(RegionTrans, "width", &W);
        RegionFeatures(RegionTrans, "row2", &Max_R);
        RegionFeatures(RegionTrans, "column2", &Max_C);


        GenRectangle2(&Rectangle1, C_R, C_C, 0, W/4, (H/2)+30);

        ReduceDomain(ImageRotate, Rectangle1, &ImageReduced1);
        EdgesSubPix(ImageReduced1, &Edges1, "canny", 3, 20, 40);
        UnionAdjacentContoursXld(Edges1, &Edges1, 10, 1, "attr_keep");
        SelectContoursXld(Edges1, &Edges1, "contour_length", (W/2)-100, (W/2)+50,
                          -0.5, 0.5);
        FitLineContourXld(Edges1, "tukey", -1, 0, 5, 2, &RowBegin1, &ColBegin1,
                          &RowEnd1, &ColEnd1, &Nr1, &Nc1, &Dist1);
        for (Index1=0; Index1<=1; Index1+=1)
        {

            CreateMetrologyModel(&MetrologyHandle1);
            AddMetrologyObjectGeneric(MetrologyHandle1, "line", ((HTuple(RowBegin1[Index1]).TupleConcat(HTuple(ColBegin1[Index1]))).TupleConcat(HTuple(RowEnd1[Index1]))).TupleConcat(HTuple(ColEnd1[Index1])),
                                      20, 5, para19, para20, HTuple(), HTuple(), &Index);
            ApplyMetrologyModel(ImageRotate, MetrologyHandle1);
            GetMetrologyObjectResult(MetrologyHandle1, "all", "all", "result_type", "all_param",
                                     &Parameter);
            ClearMetrologyModel(MetrologyHandle1);
            if (0 != (Index1==0))
            {
                Line1 = Parameter;
            }
            else
            {
                Line2 = Parameter;
            }
        }

        DistancePl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line2[0]), HTuple(Line2[1]),
                HTuple(Line2[2]), HTuple(Line2[3]), &D1);
        DistancePl(HTuple(Line1[2]), HTuple(Line1[3]), HTuple(Line2[0]), HTuple(Line2[1]),
                HTuple(Line2[2]), HTuple(Line2[3]), &D2);
        DD1 = ((D1+D2)/2)*para1+para3;
        if(!((DD1<para4+para5)&&(DD1>para4-para6)))
        {
			if (DD1 < (para4 - para6))
			{
				Flag11 = false;
			}
            Flag=false;
            flag=1;

        }
        TupleConcat(OutPara, DD1, &OutPara);
        return flag;
    }
    catch(HException ex)
    {
        Flag=false;
        OutPara.Append(0);
        return 2;

    }


}
